package com.elecbook.content.api.controller;


import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.result.Result;
import com.elecbook.base.util.SecurityUtil;
import com.elecbook.content.api.config.data.RedisContentCachePrefix;
import com.elecbook.content.model.dto.ContributePQuery;
import com.elecbook.content.model.dto.ContributePageRequestDTO;
import com.elecbook.content.model.dto.ContributeSubmitDTO;
import com.elecbook.content.model.po.Contribute;
import com.elecbook.content.service.ContributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;


@RestController("ContributeController")
@RequestMapping("/contribute")
@Slf4j
public class ContributeController {

    @Autowired
    private ContributeService contributeService;


    /**
     * 上传一个稿件
     *
     * @param dto
     * @return
     */
    @Caching(evict = {
//            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA, key = "#dto.id"),
            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA_PAGE, allEntries = true),
    })
    @PostMapping("/submit")
    public Result<Contribute> submit(@RequestBody ContributeSubmitDTO dto) {
        log.info("用户上传稿件：{}", dto);
        Contribute contributeDb = contributeService.submitContribute(dto);
        return Result.success(contributeDb);
    }

    /**
     * description 删除稿件
     *
     * @author zken
     * @CreateDate 2024/11/3 19:20:04
     */
    @Caching(evict = {
            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA, key = "#contribute.id"),
            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA_PAGE, allEntries = true),
    })
    @DeleteMapping("/quit")
    public Result<String> delete(@RequestBody Contribute contribute) {
        log.info("用户id：{}", SecurityUtil.getCurrentId());
        log.info("用户删除稿件：{}", contribute);
        contributeService.deleteContributeById(contribute);
        return Result.success();
    }


    /**
     * 分页查询方法
     *
     * @param contributePageRequestDTO
     */
    @PostMapping("/page")
    public Result<PageDTO<Contribute>> pageRequest(@RequestBody ContributePageRequestDTO contributePageRequestDTO) {
        contributePageRequestDTO.setUserId(SecurityUtil.getCurrentId());
        PageDTO<Contribute> pageResult = contributeService.queryContributePage(contributePageRequestDTO);
        return Result.success(pageResult);
    }

    /**
     * // TODO 访问限制添加
     * 分页查询方法--list使用带限制的list方法进行查询
     *
     * @param contributePageRequestDTO
     * @return
     */
    @Cacheable(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA_PAGE,
            key = "#contributePageRequestDTO.pageNo+':'+#contributePageRequestDTO.pageSize")
    @PostMapping("/open/list")
    public Result<PageDTO<Contribute>> list(@RequestBody ContributePageRequestDTO contributePageRequestDTO) {
        contributePageRequestDTO.setUserId(null);
        PageDTO<Contribute> pageResult = contributeService.queryContributePage(contributePageRequestDTO);
        return Result.success(pageResult);
    }

    /**
     * 更新稿件内容
     *
     * @param dto
     */
    @Caching(evict = {
            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA, key = "#dto.id"),
            @CacheEvict(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA_PAGE, allEntries = true),
    })
    @PutMapping("")
    public Result<String> update(@RequestBody ContributeSubmitDTO dto) {
        log.info("用户更新稿件：{}", dto);
        contributeService.updateContributeByID(dto);
        return Result.success();
    }

    /**
     * description 根据id获取内容
     *
     * @author zken
     * CreateDate 2024/11/3 19:13:17
     */
    @Cacheable(cacheNames = RedisContentCachePrefix.CONTRIBUTE_DATA,
            key = "#contributeId")
    @PostMapping("/{contributeId}")
    public Result<Contribute> getByContributeId(@PathVariable Integer contributeId) {
        log.info("学生查看contributeId = {}的稿件", contributeId);
        Contribute contribute = contributeService.getContributeById(contributeId);
        return Result.success(contribute);
    }

    /*
    管理员op操作区域
     */

    @PostMapping("/op/save")
    Result<Contribute> save(@RequestBody Contribute contribute) {
        Contribute res = contributeService.saveContribute(contribute);
        return Result.success(res);
    }

    @DeleteMapping("/op/{id}")
    Result<String> delete(@PathVariable Long id) {
        contributeService.removeById(id);
        return Result.success();
    }

    @GetMapping("/op/{id}")
    Result<Contribute> get(@PathVariable Long id) {
        Contribute res =  contributeService.getById(id);
        return Result.success(res);
    }

    @PutMapping("/op/update")
    Result<Contribute> update(@RequestBody Contribute contribute) {
        Contribute res =  contributeService.updateContributeById(contribute);
        return Result.success(res);
    }

    @PostMapping("/op/page")
    Result<PageDTO<Contribute>> page(@RequestBody ContributePQuery contributeQuery) {
        PageDTO<Contribute> pageResult = contributeService.pageQuery(contributeQuery);
        return Result.success(pageResult);
    }
}
